﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LUStationery.Controller;
using LUStationery.Model;
using System.Globalization;

public partial class MakePO : System.Web.UI.Page
{
    SupplierControl suppliercont;
    OrdersControl orderscont;
    UsersControl usercont;
    PoControl pocont;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            suppliercont = new SupplierControl();
            ddlSupplier.DataSource = suppliercont.getSuppliers();
            ddlSupplier.DataTextField = "SupplierName";
            ddlSupplier.DataValueField = "SupplierName";
            ddlSupplier.DataBind();

            string supplier = ddlSupplier.SelectedValue;
            string status = "pending";
            orderscont = new OrdersControl();
            gvOrderList.DataSource = orderscont.getOrdersBySupplierAndStatus(supplier, status);
            gvOrderList.DataBind();

            usercont = new UsersControl();
            string deptCode = "Store";
            ddlDeliverTo.DataSource = usercont.getUsersByDeptCode(deptCode);
            ddlDeliverTo.DataTextField = "username";
            ddlDeliverTo.DataValueField = "username";
            ddlDeliverTo.DataBind();
        }
    }


    protected void ddlSupplier_SelectedIndexChanged(object sender, EventArgs e)
    {
        string supplier = ddlSupplier.SelectedValue;
        string status = "pending";
        orderscont = new OrdersControl();
        gvOrderList.DataSource = orderscont.getOrdersBySupplierAndStatus(supplier, status);
        gvOrderList.DataBind();
    }
    protected void btnMakePO_Click(object sender, EventArgs e)
    {

        Response.Write("datetime.now" + DateTime.Now);
        Response.Write(txtDate.Text);
        string supplierName = ddlSupplier.SelectedValue;
        string deliverTo = ddlDeliverTo.SelectedValue;
        string attn = txtAttn.Text;

        DateTime expectedDate = Convert.ToDateTime(txtDate.Text);
        string orderBy = ((User)Session["user"]).UserName;
        pocont = new PoControl();
        ArrayList orderIDs = new ArrayList();
        foreach (GridViewRow row in gvOrderList.Rows)
        {
            int orderID = int.Parse(row.Cells[0].Text);
            orderIDs.Add(orderID);

        }

        string poID = pocont.makePo(supplierName, deliverTo, attn, expectedDate, orderBy, orderIDs);
        Response.Write(poID);

    }
}
